#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<vector<int>> threeSum(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    vector<vector<int>> ret;
    int n = nums.size();
    for (int i = 0; i < n;)
    {
        if (nums[i] > 0) break;
        int left = i + 1, right = n - 1;
        int num = -nums[i];
        while (left < right)
        {
            if (nums[left] + nums[right] < num) left++;
            else if (nums[left] + nums[right] > num) right--;
            else {
                ret.push_back({ nums[left],nums[right],nums[i] });
                left++;
                right--;
                while (left < right && nums[left] == nums[left - 1]) left++;
                while (left < right && nums[right] == nums[right + 1]) right--;
            }
        }
        i++;
        while (i < n && nums[i] == nums[i - 1]) i++;
    }
    return ret;
}

int main()
{
    vector<int> v = { -1,0,1,2,-1,-4 };

    vector<vector<int>> vv = threeSum(v);
    for (int i = 0; i < vv.size(); i++)
    {
        for (int j = 0; j < vv[i].size(); j++)
        {
            cout << vv[i][j] << ' ';
        }
        cout << endl;
    }

    return 0;
}